package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.entity.OrganizationMember;
import com.example.exception.BusinessException;
import com.example.mapper.OrganizationMemberMapper;
import com.example.service.OrganizationMemberService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Arrays;

@Service
@RequiredArgsConstructor
public class OrganizationMemberServiceImpl extends ServiceImpl<OrganizationMemberMapper, OrganizationMember> implements OrganizationMemberService {

    @Override
    @Transactional
    public boolean joinOrganization(OrganizationMember member) {
        // 检查是否已经是成员
        if (isMember(member.getOrganizationId(), member.getVolunteerId())) {
            throw new BusinessException("已经是该组织的成员");
        }
        
        // 设置初始状态
        member.setStatus(0);  // 待审核
        member.setRole(0);    // 普通成员
        
        return save(member);
    }

    @Override
    @Transactional
    public boolean reviewMember(Long id, Integer status, String rejectReason, Long reviewerId) {
        // 检查成员是否存在
        OrganizationMember member = getById(id);
        if (member == null) {
            throw new BusinessException("成员不存在");
        }
        
        // 检查状态是否合法
        if (status < 0 || status > 3) {
            throw new BusinessException("状态值不合法");
        }
        
        // 更新审核信息
        member.setStatus(status);
        member.setRejectReason(rejectReason);
        member.setReviewerId(reviewerId);
        member.setReviewTime(LocalDateTime.now());
        
        return updateById(member);
    }

    @Override
    @Transactional
    public boolean updateMember(OrganizationMember member) {
        // 检查成员是否存在
        OrganizationMember existingMember = getById(member.getId());
        if (existingMember == null) {
            throw new BusinessException("成员不存在");
        }
        
        // 检查状态是否允许修改
        if (existingMember.getStatus() == 2 || existingMember.getStatus() == 3) {
            throw new BusinessException("当前状态不允许修改");
        }
        
        return updateById(member);
    }

    @Override
    @Transactional
    public boolean updateMemberRole(Long id, Integer role) {
        // 检查成员是否存在
        OrganizationMember member = getById(id);
        if (member == null) {
            throw new BusinessException("成员不存在");
        }
        
        // 检查角色是否合法
        if (role < 0 || role > 2) {
            throw new BusinessException("角色值不合法");
        }
        
        member.setRole(role);
        return updateById(member);
    }

    @Override
    @Transactional
    public boolean toggleMemberStatus(Long id, boolean enable) {
        // 检查成员是否存在
        OrganizationMember member = getById(id);
        if (member == null) {
            throw new BusinessException("成员不存在");
        }
        
        member.setStatus(enable ? 1 : 3);  // 1-已审核 3-已禁用
        return updateById(member);
    }

    @Override
    @Transactional
    public boolean removeMember(Long id) {
        return removeById(id);
    }

    @Override
    @Transactional
    public boolean batchRemoveMembers(Long[] ids) {
        return removeByIds(Arrays.asList(ids));
    }

    @Override
    public Page<OrganizationMember> getMemberList(Integer pageNum, Integer pageSize, Long organizationId, 
            Long volunteerId, Integer role, Integer status) {
        Page<OrganizationMember> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<OrganizationMember> wrapper = new LambdaQueryWrapper<>();
        
        // 构建查询条件
        if (organizationId != null) {
            wrapper.eq(OrganizationMember::getOrganizationId, organizationId);
        }
        if (volunteerId != null) {
            wrapper.eq(OrganizationMember::getVolunteerId, volunteerId);
        }
        if (role != null) {
            wrapper.eq(OrganizationMember::getRole, role);
        }
        if (status != null) {
            wrapper.eq(OrganizationMember::getStatus, status);
        }
        
        wrapper.orderByDesc(OrganizationMember::getCreateTime);
        return page(page, wrapper);
    }

    @Override
    public OrganizationMember getMemberInfo(Long id) {
        OrganizationMember member = getById(id);
        if (member == null) {
            throw new BusinessException("成员不存在");
        }
        return member;
    }

    @Override
    public boolean isMember(Long organizationId, Long volunteerId) {
        LambdaQueryWrapper<OrganizationMember> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(OrganizationMember::getOrganizationId, organizationId)
                .eq(OrganizationMember::getVolunteerId, volunteerId)
                .eq(OrganizationMember::getStatus, 1);  // 已审核状态
        return count(wrapper) > 0;
    }

    @Override
    public boolean isAdmin(Long organizationId, Long volunteerId) {
        LambdaQueryWrapper<OrganizationMember> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(OrganizationMember::getOrganizationId, organizationId)
                .eq(OrganizationMember::getVolunteerId, volunteerId)
                .eq(OrganizationMember::getStatus, 1)    // 已审核状态
                .in(OrganizationMember::getRole, 1, 2);  // 管理员或组织者角色
        return count(wrapper) > 0;
    }
} 